/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "lnmline.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

float maxi(float v1, float v2){
	if(v1>v2) return v1;
	else return v2;
}
float mini(float v1, float v2){
	if(v1<v2) return v1;
	else return v2;
}

int main(int argc, char *argv[]){
		srand(time(NULL));
		lnm::TLineMap map;
		int num_of_extra_lines=0;
		int remove_level = 0;
		float sx = ((float)rand()/RAND_MAX - 0.5) * 20.0;
		float size = 6.0;
		
		if(argc >1) num_of_extra_lines = atoi(argv[1]);
		if(argc >2) remove_level = atoi(argv[2]);
		if(remove_level > 6){
				size=remove_level;
				remove_level = 6;
		}
		
		fprintf(stderr,"Parsin linemap with %d extralines and %d remove level\n",num_of_extra_lines,remove_level);
		
		//map.readMapFile("tuas2k.txt",1.0);
		//map.readMapFile("tuas3k_lines.txt",1.0/1000.0);
		map.readMapFile("rocla-line.txt",1.0/1000.0);
		//FILE *f = fopen("autmap-strip2.txt","wt");
		
#if 1
		//map.readMapFile("ala-aula-1510-2.txt",1.0/1000.0);
		
		//file:///home/jari/softa/svgparser/kartta_loppudemo.txt
		//FILE *f = fopen("aula.txt","wt");
		FILE *f = fopen("rocla.txt","wt");
		
		for(int i=0;i<map.NumOfLines;i++){
				if(map.Lines[i].y1 > 20){
				
				
				map.Lines[i].x1 -= 100;
				map.Lines[i].x2 -= 100;
				map.Lines[i].y1 -= 100;
				map.Lines[i].y2 -= 100;
				/*if(map.Lines[i].lenght()<50)*/
				fprintf(f,"%f %f %f %f\n",map.Lines[i].x1,map.Lines[i].y1,map.Lines[i].x2,map.Lines[i].y2);
				}
				
		}
		fclose(f);
#else
		FILE *f = fopen("autmap-strip2.txt","wt");
		for(int i=0;i<map.NumOfLines;i++){
				
				map.Lines[i].x1 -= 100;
				map.Lines[i].x2 -= 100;
				map.Lines[i].y1 -= 42;
				map.Lines[i].y2 -= 42;
			
				if(maxi(map.Lines[i].x1,map.Lines[i].x2)>-35 && maxi(map.Lines[i].y1,map.Lines[i].y2)>-35){
						if(mini(map.Lines[i].x1,map.Lines[i].x2)<55 && mini(map.Lines[i].y1,map.Lines[i].y2)<55){
								
								bool nadd = false;
								for(int r=0;r<remove_level;r++){
										nadd |= (( maxi(map.Lines[i].x1,map.Lines[i].x2)> sx + 10*r && 
														 maxi(map.Lines[i].y1,map.Lines[i].y2)>0) &&
													 (mini(map.Lines[i].x1,map.Lines[i].x2)<sx+size + 10*r && 
														mini(map.Lines[i].y1,map.Lines[i].y2)<size));
																		
								}
								
								if(!nadd) fprintf(f,"%f %f %f %f\n",map.Lines[i].x1,map.Lines[i].y1,map.Lines[i].x2,map.Lines[i].y2);
								
								//fprintf(f,"%f %f %f %f\n",map.Lines[i].x1,map.Lines[i].y1,map.Lines[i].x2,map.Lines[i].y2);
// 								if( ( maxi(map.Lines[i].x1,map.Lines[i].x2)>0 && maxi(map.Lines[i].y1,map.Lines[i].y2)>0) &&
// 													 (mini(map.Lines[i].x1,map.Lines[i].x2)<5 && mini(map.Lines[i].y1,map.Lines[i].y2)<5))
// 								{		
// 								}
// 								else if( ( maxi(map.Lines[i].x1,map.Lines[i].x2)>15 && maxi(map.Lines[i].y1,map.Lines[i].y2)>0) &&
// 																	 (mini(map.Lines[i].x1,map.Lines[i].x2)<25 && mini(map.Lines[i].y1,map.Lines[i].y2)<5))
// 								{		
// 								}
// 								else if( ( maxi(map.Lines[i].x1,map.Lines[i].x2)>25 && maxi(map.Lines[i].y1,map.Lines[i].y2)>0) &&
// 																	 (mini(map.Lines[i].x1,map.Lines[i].x2)<35 && mini(map.Lines[i].y1,map.Lines[i].y2)<10))
// 								{		
// 								}  							
// 								else{
// 										fprintf(f,"%f %f %f %f\n",map.Lines[i].x1,map.Lines[i].y1,map.Lines[i].x2,map.Lines[i].y2);
// 								}
						}
				}
				
				
				/*
				if(maxi(map.Lines[i].x1,map.Lines[i].x2)>-25 && maxi(map.Lines[i].y1,map.Lines[i].y2)>-15){
					if(mini(map.Lines[i].x1,map.Lines[i].x2)<25 && mini(map.Lines[i].y1,map.Lines[i].y2)<15){
							fprintf(f,"%f %f %f %f\n",map.Lines[i].x1,map.Lines[i].y1,map.Lines[i].x2,map.Lines[i].y2);
						}
				
					}*/
				
		}
		
		
		for(int i=0;i<num_of_extra_lines;i++){
				fprintf(f,"%f %f %f %f\n",
								( (float)rand()/RAND_MAX-0.5)*50.0,
								((float)rand()/RAND_MAX-0.5)*50.0,
								((float)rand()/RAND_MAX-0.5)*50.0,
								((float)rand()/RAND_MAX-0.5)*50.0);
		}
		fclose(f);
#endif
		
		
		/** XML **
		map.save("autmap-koko.txt"); //XML("multi-map.xml","walls", "wall");
		*/
	return 0;
}
